<?php

/**
 * Classe responsavel por todos os topicos do sistema.
 * @author Emeson Santana
 * @package app.Controller
 */
Class TopicsController extends AppController{
	public $helpers = array('Html', 'Form');
	public $name = 'Topics';
	public $uses = array('Topic','Community');

	public function add($community_id = null){
		$this->layout = 'ajax';
		if($this->request->is('post')){
			//date_default_timezone_set('America/Recife');
			$this->request->data['Topic']['creation_date'] = date("Y-m-d H:i:s");
			$this->request->data['Topic']['url'] = $this->StrToUrl($this->request->data['Topic']['name']);
			
			if($this->Topic->saveAll($this->request->data)){
				$this->Topic->Post->set(array(
					"description" => $_POST['description'],
					"creation_date" => $this->request->data['Topic']['creation_date'],
					"user_id" => $this->request->data['Topic']['user_id'],
					"topic_id" => $this->Topic->id
					));
				if ($this->Topic->Post->save()) {
					$this->Session->setFlash(__('<span class="label label-success">Sucesso!</span>'));
					$this->redirect('/!'.$this->Community->getUrlById($this->request->data['Topic']['community_id']));
				}
			}
			else{
				$this->Session->setFlash(__('<span class="label label-warning">Erro!</span>'));
			}
		}
		else{
			$this->request->data['Topic']['community_id'] = $community_id;
			$this->set('community_id', $community_id);
		}

	}
	public function edit($community_id, $id){
		$this->Topic->id = $id;
        if ($this->request->is('get')) {
				$this->request->data = $this->Topic->read();
				$this->set('community_id', $community_id);
		}
		else {
			$this->Topic->id = $id;

			if ($this->Topic->saveAll($this->request->data)) {
				$this->Session->setFlash(__('Sucesso!'));
				$this->redirect(array('action' => '../communities/view', $community_id));
			}
		}
	}
	public function delete($community_id, $id){

		if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Topic->id = $id;
        if (!$this->Topic->exists()) {
            throw new NotFoundException(__('<span class="label label-warning">Tópico Inválido</span>'));
        }
        if ($this->Topic->delete()) {
            $this->Session->setFlash(__('<span class="label label-success">Tópico deletado</span>'));
            $this->redirect(array('action' => '/communities/view', $community_id));
        }
        $this->Session->setFlash(__('Topic was not deleted'));
        $this->redirect(array('action' => '/communities/view', $community_id));
		
	}
	public function view($community_url, $url){
		$this->layout = 'communit';
		$id = $this->Topic->getIdByUrl($url);
		$this->Topic->id = $id;

		$posts = $this->Topic->Post->find('all', array('conditions' => array('topic_id' => $id), 'order' => array('Post.creation_date')));

	    if ($this->request->is('get')) {
	        $this->set('topic', $this->Topic->read());
	        $this->set('posts', $posts);
	        $this->set('cmm_url',$community_url);
	    }
	}
}